# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# NOTE: This file is auto generated by the elixir code generator program.
# Do not edit this file manually.

defmodule <%= namespace %>.Api.<%= api.name %> do
  @moduledoc """
  <%= render_description(api.description, indent_subsequent: 2) %>
  """

  alias <%= namespace %>.Connection
  alias GoogleApi.Gax.{Request, Response}

  @library_version Mix.Project.config() |> Keyword.get(:version, "")

  <%= for endpoint <- api.endpoints do %>
  @doc """
  <%= render_description(endpoint.description, indent_subsequent: 2) %>

  ## Parameters

  *   `connection` (*type:* `<%= namespace %>.Connection.t`) - Connection to server<%= for parameter <- endpoint.required_parameters do %>
  *   `<%= parameter.variable_name %>` (*type:* `<%= parameter.type.typespec %>`) - <%= render_description(parameter.description, indent_subsequent: 6) %><% end %>
  *   `optional_params` (*type:* `keyword()`) - Optional parameters<%= for parameter <- global_optional_parameters ++ endpoint.optional_parameters do %>
      *   `<%= render_atom(parameter.name) %>` (*type:* `<%= parameter.type.typespec %>`) - <%= render_description(parameter.description, indent_subsequent: 10) %><% end %>
  *   `opts` (*type:* `keyword()`) - Call options

  ## Returns

  *   `{:ok, %<%= endpoint.return.struct %>{}}` on success
  *   `{:error, info}` on failure
  """
  @spec <%= endpoint.typespec %>
  def <%= endpoint.name %>(connection, <%= for parameter <- endpoint.required_parameters do %><%= parameter.variable_name %>, <% end %>optional_params \\ [], opts \\ []) do
    <%= if endpoint.is_download do %>
    opts = if Keyword.get(optional_params, :alt) == "media", do: Keyword.put_new(opts, :decode, false), else: opts
    <% end %>
    optional_params_config = %{
    <%= for parameter <- global_optional_parameters do %>
      :"<%= parameter.name%>" => :"<%= parameter.location %>",
    <% end %>
    <%= for parameter <- endpoint.optional_parameters do %>
      :"<%= parameter.name%>" => :"<%= parameter.location %>",
    <% end %>
    }

    request =
      Request.new()
      |> Request.method(:"<%= endpoint.method %>")
      |> Request.url("<%= endpoint.path %>"<%= if endpoint.path_parameters do %>, %{<%= for parameter <- endpoint.path_parameters do %>
        "<%= parameter.name %>" => <%= if parameter.type.name == "string" do %>URI.encode(<%= parameter.variable_name %>, <%= if parameter.is_path_trailer do %>&(URI.char_unreserved?(&1) || &1 == ?/)<% else %>&URI.char_unreserved?/1<% end %>)<% else %><%= parameter.variable_name %><% end %>,
      <% end %>}<% end %>)
      <%= for parameter <- endpoint.required_parameters do %><%= if parameter.location != "path" do %>
      |> Request.add_param(:"<%= parameter.location %>", :"<%= parameter.name %>", <%= parameter.variable_name %>)
      <% end %><% end %>
      |> Request.add_optional_params(optional_params_config, optional_params)
      |> Request.library_version(@library_version)

    connection
    |> Connection.execute(request)
    |> Response.decode(opts ++ [<%= if endpoint.return.struct do %>struct: %<%= endpoint.return.struct %>{}<% else %>decode: false<% end %><%= if data_wrapped do %>, data_wrapped: true<% end %>])
  end
  <% end %>
end
